package com.handpay.zztong.hp.klf;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.log.HPLog;

import java.util.Vector;

public class PWDScreen2 extends LinearLayout
{
	private static final int[] imageIds = new int[]{R.id.keyword_0,R.id.keyword_1,R.id.keyword_2,R.id.keyword_3,R.id.keyword_4,R.id.keyword_5};
	private static ImageView[] imageViews = new ImageView[6];
	private static final int[] idButtonNumbers = new int[]{R.id.btnNum0,R.id.btnNum1,R.id.btnNum2,R.id.btnNum3,R.id.btnNum4,R.id.btnNum5,R.id.btnNum6,R.id.btnNum7,R.id.btnNum8,R.id.btnNum9};
	private boolean isCheck = true;//是否检测密码规则
	private Context context;
	private LinearLayout layout;
	private String msg;
	private int maxlen = 6,minlen = 6;
	private TextView etPassword;
	private TextView textWarning;
	private TextView tvInfo,mDialogText;
	private Button btnNumbers[];
	private ImageButton ivNumDelete,ivNumOK,ibNumCancle;
	private TableLayout tlNumber,tlLetter;
	private ClickListener clickListener;
	private View btnPwdBack;
	private View showAmountView;
	private TextView amount;
	private View close;
	private CloseListener closeListener;
	public PWDScreen2(Context context,AttributeSet arrt)
	{
		super(context,arrt);
		this.context = context;
		LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = (LinearLayout)inflate.inflate(R.layout.layout_security_key_board, null);
		showAmountView = layout.findViewById(R.id.showAmountView);
		amount = (TextView) layout.findViewById(R.id.amount);
		//layout.findViewById(R.id.tvTitle).setOnClickListener(context);
		btnNumbers = new Button[idButtonNumbers.length];
		for(int i = 0;i < idButtonNumbers.length;i ++)
		{
			btnNumbers[i] = (Button)layout.findViewById(idButtonNumbers[i]);
		}
		ivNumDelete = (ImageButton)layout.findViewById(R.id.ibNumDelete);
		ivNumOK = (ImageButton)layout.findViewById(R.id.btnOk);
		ibNumCancle = (ImageButton)layout.findViewById(R.id.ibNumCancle);
		textWarning = (TextView) layout.findViewById(R.id.text_warning);
		btnPwdBack = layout.findViewById(R.id.btnPwdBack);
		for (int i = 0; i<6 ; i++) {
			imageViews[i]=(ImageView) layout.findViewById(imageIds[i]);
		}
		textWarning.setText("请顾客输入银行卡密码");
		
		tvInfo = (TextView)layout.findViewById(R.id.tvInfo);
		close = layout.findViewById(R.id.close);
		etPassword = (TextView)layout.findViewById(R.id.tfPwd);
		
		
		if(TextUtils.isEmpty(msg))
		{
			tvInfo.setVisibility(View.GONE);
		}
		else
		{
			tvInfo.setVisibility(View.VISIBLE);
			tvInfo.setText(msg);
		}
		
		etPassword.setInputType(InputType.TYPE_NULL);
		etPassword.setTransformationMethod(new PasswordTransformationMethod());
		
		final OnTouchListener otlKey = new OnTouchListener()
		{

			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				Button btn = (Button)v;
				String text = btn.getText().toString();
				switch(event.getAction())
				{
					case MotionEvent.ACTION_DOWN:
						showKey(true,text);
						break;
					case MotionEvent.ACTION_UP:
					case MotionEvent.ACTION_OUTSIDE:
						showKey(false,text);
						break;
				}
				return false;
			}
			
		};		
		if(btnNumbers != null)
		{
			Vector<String> vNums = new Vector<String>();
			for (int i = 0; i < 10; i++)
			{
				vNums.add("" + i);
			}
			for(int i = 0;i < btnNumbers.length;i ++)
			{
				String text = getRandom(vNums);//乱序代码部分
//				String text = vNums.get(i);
				btnNumbers[i].setText(text);
				btnNumbers[i].setOnClickListener(new OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						Button button = (Button)v;
						doPassword(button.getText().toString());
					}
					
				});
				btnNumbers[i].setOnTouchListener(otlKey);
			}
		}
		
		
		final OnClickListener oclOK = new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				doOK();
			}
			
		};
		if(ivNumOK != null)
		{
			ivNumOK.setOnClickListener(oclOK);
		}
		
		final OnClickListener oclDelete = new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				doClear();
			}
			
		};
		if(ivNumDelete != null)
		{
			ivNumDelete.setOnClickListener(oclDelete);
		}
		if(ibNumCancle != null)
		{
			ibNumCancle.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					while(etPassword.length()>0){
						doClear();
					}
				}
			});
		}
		
		btnPwdBack.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View arg0)
			{
				if (clickListener != null) {
					clickListener.onClick(0, null);
				}
			}
		});
		
		if (close != null) {
			close.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View arg0)
				{
					if (closeListener != null) {
						closeListener.onClick();
					}
				}
			});
		}
		this.addView(layout);
		
	}
	private String getRandom(Vector<String> temss)
	{
		if (temss != null && temss.size() > 0)
		{
			double dr = Math.random();
			int tsize = temss.size();
			int num = (int) (dr * tsize);
			String ss = temss.elementAt(num);
			temss.remove(num);
			return ss;
		}
		return null;
	}
	
	public void amountValueVisibily(int visibility,String amountValue) {
		amount.setText("¥ " + amountValue);
		showAmountView.setVisibility(visibility);
	}
	private void showKey(final boolean show, final String text)
	{
		
		class ShowKey implements Runnable
		{

			@Override
			public void run()
			{
				mDialogText.setText(text);
				mDialogText.setVisibility(show?View.VISIBLE:View.INVISIBLE);
			}
			
		}
		new ShowKey();
	}
	
	
	/**
	 * 输入密码
	 * 
	 * @param key
	 */
	private void doPassword(String key)
	{
		String password = "";
		if (etPassword != null/* && etPassword.isFocused()*/)
		{
			password = etPassword.getText().toString();
		}
		if (password.length() < this.maxlen)
		{
			if (etPassword != null)
			{
				etPassword.setText(password + key);
			}
		}
		if(password.length() < 6){
			ImageView view = imageViews[etPassword.getText().toString().length()-1];
			view.setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * 按确认键
	 */
	private void doOK()
	{
		String password = "";
		if(etPassword != null)
		{
			password = etPassword.getText().toString();
		}
		if (password.length() < minlen)
		{
			showAlertDialog("错误原因：密码至少" + minlen + "位。");
			return;
		}
		HPLog.i("Test", "确认按钮====password=" + password);
		if (clickListener != null) {
			clickListener.onClick(1, password);
		}
	}

	/**
	 * 删除前一个字符
	 */
	private void doClear()
	{
		String password = "";
		if (etPassword != null/* && etPassword.isFocused()*/)
		{
			password = etPassword.getText().toString();
			if (password.length() > 0)
			{
				etPassword.setText(password.substring(0,
						password.length() - 1));
			}
		}
		final ImageView view = imageViews[password.length()-1 < 0 ? 0:password.length()-1];
		if(password.length() !=0){
			AlphaAnimation ani = new AlphaAnimation(1.0f, 0.0f);
			ani.setDuration(200);
			view.startAnimation(ani);
			view.setVisibility(View.GONE);
		}
	}
	
	private DialogInterface.OnClickListener oclAlertOK;
	protected final DialogInterface.OnClickListener oclNull = new DialogInterface.OnClickListener()
	{

		@Override
		public void onClick(DialogInterface dialog, int which)
		{
			dialog.dismiss();
		}
		
	};
	
	private Handler	hShowAlertDialog	= new Handler()
	{
		
		@Override
		public void handleMessage(
				Message msg)
		{
			Bundle data = msg.getData();
			String title = data
					.getString("title");
			String message = data
					.getString("msg");
			
			new AlertDialog.Builder(
					context)
					.setTitle(title)
					.setMessage(message)
					.setPositiveButton(PWDScreen2.this.getResources().getString(android.R.string.ok), oclAlertOK)
					.create()
					.show();
		}
	};
	
	/**
	 * 显示AlertDialog
	 * 
	 * @param title
	 * @param msg
	 */
	protected void showAlertDialog(String title, String msg, DialogInterface.OnClickListener ok)
	{		
		if(ok == null)
		{
			oclAlertOK = oclNull;
		}
		else
		{
			oclAlertOK = ok;
		}
		
		Message message = new Message();
		Bundle data = new Bundle();
		data.putString("title", title);
		data.putString("msg", msg);
		message.setData(data);
		hShowAlertDialog.sendMessage(message);
	}
	
	protected void showAlertDialog(String msg)
	{
		showAlertDialog(this.getResources().getString(android.R.string.dialog_alert_title),
				msg, null);
	}
	
	public interface ClickListener {
		void onClick(int type,String pwd);
	}
	
	public void setPwdClickListener(ClickListener clickListener) {
		this.clickListener = clickListener;
	}
	
	public interface CloseListener {
		void onClick();
	}
	public void setCloseListener(CloseListener closeListener) {
		this.closeListener = closeListener;
	}
	
}
